package com.yz.hoo;

import com.alibaba.fastjson.JSONObject;
import com.yz.common.R;
import com.yz.util.EmptyUtil;
import com.yz.util.OkHttpUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@Api("向HOO发送请求相关的api")
public class HooApiController {
    public static final String sfrom = "DChain";
    public static final String key = "12345678";
    public static final String callBack_ip = "http://13.251.220.99:8080";
    public static final String ip = "https://f.hoo.com/v2/";
    public static final String test_ip = "http://47.75.17.17/v2/";
    public static final String path_applyAddress = "applyAddress";
    public static final String path_getApplyAddress = "getApplyAddress";
    public static final String path_getAddressApplyList = "getAddressApplyList";
    public static final String path_applyTransaction = "applyTransaction";
    public static final String path_getApplyTransaction = "getApplyTransaction";
    public static final String path_getTransactionApplyList = "getTransactionApplyList";
    public static final String path_applyBuyRam = "applyBuyRam";
    public static final String path_applySellRam = "applySellRam";
//    public static final String path_getApplyRam = "getApplyRam";
    public static final String path_getRamApplyList = "getEosRamApplyList";
//    public static final String path_getPost = "getPost";
    private static final Logger logger = LoggerFactory.getLogger(HooApiController.class);

    public HooApiController() {
    }

//    @ApiOperation(
//            value = "测试连接是否联通",
//            notes = "发送请求测试连接是否联通"
//    )
//    @GetMapping({path_getPost})
//    public Object getPost(@RequestParam(required = false) Map<String, Object> params) {
//        logger.info("{}：参数：{}", path_getPost, params);
//        String json = OkHttpUtil.postFormParams(HooUtil.getpath(path_getPost, ip), HooUtil.getParamMap(params, key));
//        logger.info("{}：返回结果：{}", path_getPost, json);
//        return R.ok("返回结果：" + json);
//    }

    @ApiOperation(
            value = "对外生成地址",
            notes = "生成相应币种的地址"
    )
    @PostMapping(path_applyAddress)
    @ApiImplicitParams({@ApiImplicitParam(
            paramType = "query",
            name = HooParamKey.coinName,
            value = "币种名称",
            required = true,
            dataType = "String",
            defaultValue = "eth")
            , @ApiImplicitParam(
            paramType = "query",
            name = HooParamKey.num,
            value = "地址生成数量",
            required = true,
            dataType = "Integer",
            defaultValue = "1"
    )})

    public Object applyAddress(@RequestParam(HooParamKey.coinName) String coinName,@RequestParam(HooParamKey.num) String num) {
        Map<String, Object> params = new HashMap();
        params.put(HooParamKey.coinName, coinName);
        params.put(HooParamKey.num, num);
        return this.applyAddress(params, path_applyAddress, test_ip, key).getData();
    }

    @ApiOperation(
            value = "通过订单号查询生成的地址",
            notes = "通过订单号查询生成的地址,一个订单号客生成多个地址"
    )
    @PostMapping(path_getApplyAddress)
    @ApiImplicitParams({@ApiImplicitParam(
            paramType = "query",
            name = HooParamKey.outOrderId,
            value = "订单号",
            required = true,
            dataType = "String",
            defaultValue = "xxxx"
    ), @ApiImplicitParam(
            paramType = "query",
            name = HooParamKey.start,
            value = "偏移量分页",
            required = true,
            dataType = "Integer",
            defaultValue = "0"
    ), @ApiImplicitParam(
            paramType = "query",
            name = HooParamKey.num,
            value = "地址生成数量",
            required = true,
            dataType = "Integer",
            defaultValue = "1"
    )})
    public Object getApplyAddress(@RequestParam(HooParamKey.outOrderId) String outOrderId, @RequestParam(HooParamKey.start) Integer start, @RequestParam(HooParamKey.num) Integer num) {
        Map<String, Object> params = new HashMap();
        params.put(HooParamKey.outOrderId, outOrderId);
        params.put(HooParamKey.start, start);
        params.put(HooParamKey.num, num);
        return this.getApplyAddress(params, path_getApplyAddress, test_ip, key).getData();
    }

    @ApiOperation(
            value = "地址申请列表",
            notes = "地址申请列表"
    )
    @PostMapping(path_getAddressApplyList)
    @ApiImplicitParams({@ApiImplicitParam(
            paramType = "query",
            name = HooParamKey.start,
            value = "偏移量分页",
            required = true,
            dataType = "Integer",
            defaultValue = "0"
    ), @ApiImplicitParam(
            paramType = "query",
            name = HooParamKey.num,
            value = "地址生成数量",
            required = true,
            dataType = "Integer",
            defaultValue = "10"
    )})
    public Object getAddressApplyList(@RequestParam(HooParamKey.start) Integer start,
                                      @RequestParam(HooParamKey.num) Integer num) {
        Map<String, Object> params = new HashMap();
        params.put(HooParamKey.start, start);
        params.put(HooParamKey.num, num);
        return this.getAddressApplyList(params, path_getAddressApplyList, test_ip, key).getData();
    }

    @ApiOperation(
            value = "自动化交易申请",
            notes = "自动化交易申请"
    )
    @PostMapping({path_applyTransaction})
    @ApiImplicitParams({@ApiImplicitParam(
            paramType = "query",
            name = HooParamKey.coinName,
            value = "币种名称",
            required = true,
            dataType = "String",
            defaultValue = "eos"
    ), @ApiImplicitParam(
            paramType = "query",
            name = HooParamKey.amount,
            value = "转账数量",
            required = true,
            dataType = "String",
            defaultValue = "0.01"
    ), @ApiImplicitParam(
            paramType = "query",
            name = HooParamKey.toAddress,
            value = "转入地址",
            required = true,
            dataType = "String",
            defaultValue = "chdxyzeos234"
    )})
    public Object applyTransaction(@RequestParam(HooParamKey.coinName) String coinName, @RequestParam(HooParamKey.amount) String amount, @RequestParam(HooParamKey.toAddress) String toAddress) {
        Map<String, Object> params = new HashMap();
        params.put(HooParamKey.coinName, coinName);
        params.put(HooParamKey.amount, amount);
        params.put(HooParamKey.toAddress, toAddress);
        JSONObject data = (JSONObject) this.applyTransaction(params, path_applyTransaction, test_ip, key).getData();
        if (data != null) {
            data.put(HooParamKey.outOrderId, params.get(HooParamKey.outOrderId));
        }

        return data;
    }

    @ApiOperation(
            value = "查询(转账，内存)交易信息",
            notes = "查询获取交易信息(包括内存、转账)"
    )
    @PostMapping({path_getApplyTransaction})
    @ApiImplicitParams({@ApiImplicitParam(
            paramType = "query",
            name = HooParamKey.outOrderId,
            value = "订单号",
            required = true,
            dataType = "String",
            defaultValue = "xxxx"
    ), @ApiImplicitParam(
            paramType = "query",
            name = HooParamKey.coinName,
            value = "币种",
            required = true,
            dataType = "String",
            defaultValue = "eos"
    )})
    public Object getApplyTransaction(@RequestParam(HooParamKey.outOrderId) String outOrderId,
                                      @RequestParam(HooParamKey.coinName) String coinName) {
        Map<String, Object> params = new HashMap();
        params.put(HooParamKey.outOrderId, outOrderId);
        params.put(HooParamKey.coinName, coinName);
        return this.getApplyTransaction(params, path_getApplyTransaction, test_ip, key).getData();
    }

    @ApiOperation(
            value = "查询获取交易申请列表",
            notes = "查询获取交易申请列表"
    )
    @PostMapping(path_getTransactionApplyList)
    @ApiImplicitParams({@ApiImplicitParam(
            paramType = "query",
            name = HooParamKey.start,
            value = "偏移量分页",
            required = true,
            dataType = "Integer",
            defaultValue = "0"
    ), @ApiImplicitParam(
            paramType = "query",
            name = HooParamKey.num,
            value = "地址生成数量",
            required = true,
            dataType = "Integer",
            defaultValue = "10"
    ), @ApiImplicitParam(
            paramType = "query",
            name = HooParamKey.coinName,
            value = "币种",
            required = true,
            dataType = "String",
            defaultValue = "eos"
    )})
    public Object getTransactionApplyList(@RequestParam(HooParamKey.start) Integer start,
                                          @RequestParam(HooParamKey.num) Integer num,
                                          @RequestParam(HooParamKey.coinName) String coinName) {
        Map<String, Object> params = new HashMap();
        params.put(HooParamKey.start, start);
        params.put(HooParamKey.num, num);
        params.put(HooParamKey.coinName, coinName);
        return this.getTransactionApplyList(params, path_getTransactionApplyList, test_ip, key).getData();
    }

    @ApiOperation(
            value = "EOS买内存",
            notes = "EOS买内存"
    )
    @PostMapping({path_applyBuyRam})
    @ApiImplicitParams({@ApiImplicitParam(
            paramType = "query",
            name = HooParamKey.coinName,
            value = "币种名称",
            required = true,
            dataType = "String",
            defaultValue = "eos"
    ), @ApiImplicitParam(
            paramType = "query",
            name = HooParamKey.amount,
            value = "转账数量",
            required = true,
            dataType = "String",
            defaultValue = "0.01"
    ), @ApiImplicitParam(
            paramType = "query",
            name = HooParamKey.coinFrom,
            value = "转出地址",
            required = true,
            dataType = "String",
            defaultValue = "chdxyzeos123"
    ), @ApiImplicitParam(
            paramType = "query",
            name = HooParamKey.coinTo,
            value = "转入地址",
            required = true,
            dataType = "String",
            defaultValue = "chdxyzeos234"
    )})
    public Object applyBuyRam(@RequestParam(HooParamKey.coinName) String coinName, @RequestParam(HooParamKey.amount) String amount,
                              @RequestParam(required = false,name = HooParamKey.coinFrom) String coinFrom, @RequestParam(HooParamKey.coinTo) String coinTo) {
        Map<String, Object> params = new HashMap();
        params.put(HooParamKey.coinName, coinName);
        params.put(HooParamKey.amount, amount);
        if(EmptyUtil.isNotEmpty(coinFrom)){
            params.put(HooParamKey.coinFrom, coinFrom);
        }
        params.put(HooParamKey.coinTo, coinTo);
        return this.applyBuyRam((Map) params, path_applyBuyRam, test_ip, key).getData();
    }

    @ApiOperation(
            value = "EOS卖内存",
            notes = "EOS卖内存"
    )
    @PostMapping({path_applySellRam})
    @ApiImplicitParams({@ApiImplicitParam(
            paramType = "query",
            name = HooParamKey.coinName,
            value = "币种名称",
            required = true,
            dataType = "String",
            defaultValue = "eos"
    ), @ApiImplicitParam(
            paramType = "query",
            name = HooParamKey.amount,
            value = "转账数量",
            required = true,
            dataType = "String",
            defaultValue = "0.01"
    ), @ApiImplicitParam(
            paramType = "query",
            name = HooParamKey.coinTo,
            value = "转入地址",
            required = true,
            dataType = "String",
            defaultValue = "chdxyzeos234"
    )})
    public Object applySellRam(@RequestParam(HooParamKey.coinName) String coinName, @RequestParam(HooParamKey.amount) String amount,
                               @RequestParam(required = false,name = HooParamKey.coinTo) String coinTo) {
        Map<String, Object> params = new HashMap();
        params.put(HooParamKey.coinName, coinName);
        params.put(HooParamKey.amount, amount);
        if(EmptyUtil.isNotEmpty(coinTo)){
            params.put(HooParamKey.coinTo,coinTo);
        }
        return this.applySellRam(params, path_applySellRam, test_ip, key).getData();
    }

//    @ApiOperation(
//            value = "查询内存交易信息",
//            notes = "查询内存交易信息"
//    )
//    @PostMapping({path_getApplyRam})
//    @ApiImplicitParams({@ApiImplicitParam(
//            paramType = "query",
//            name = HooParamKey.outOrderId,
//            value = "订单号",
//            required = true,
//            dataType = "String",
//            defaultValue = "xxxx"
//    ), @ApiImplicitParam(
//            paramType = "query",
//            name = HooParamKey.num,
//            value = "订单号",
//            required = true,
//            dataType = "Integer",
//            defaultValue = "10"
//    )})
//    @Deprecated
//    public Object getApplyRam(@RequestParam(HooParamKey.outOrderId) String outOrderId, @RequestParam(HooParamKey.num) String num) {
//        Map<String, Object> params = new HashMap();
//        return this.getApplyRam(params, path_getApplyRam, test_ip, key).getData();
//    }

    @ApiOperation(
            value = "查询eos内存买卖列表",
            notes = "查询eos内存买卖列表"
    )
    @PostMapping({path_getRamApplyList})
    @ApiImplicitParams({@ApiImplicitParam(
            paramType = "query",
            name = HooParamKey.start,
            value = "偏移量分页",
            required = true,
            dataType = "Integer",
            defaultValue = "0"
    ), @ApiImplicitParam(
            paramType = "query",
            name = HooParamKey.num,
            value = "地址生成数量",
            required = true,
            dataType = "Integer",
            defaultValue = "10"
    ), @ApiImplicitParam(
            paramType = "query",
            name = HooParamKey.coinName,
            value = "填(eosrambuy或eosramsell)",
            required = true,
            dataType = "String",
            defaultValue = "1"
    )})
    public Object getRamApplyList(@RequestParam(HooParamKey.start) Integer start, @RequestParam(HooParamKey.num) Integer num,
                                  @RequestParam(value = HooParamKey.coinName, required = false) String coinName) {
        Map<String, Object> params = new HashMap();
        params.put(HooParamKey.start, start);
        params.put(HooParamKey.num, num);
        if (EmptyUtil.isNotEmpty(coinName)) {
            params.put(HooParamKey.coinName, coinName);
        }
        return this.getRamApplyList(params, path_getRamApplyList, test_ip, key).getData();
    }

    private R<JSONObject> getRamApplyList(Map<String, Object> params, String path, String ip, String key) {
        logger.info("{}：参数：{}", path, params);
        params.put(HooParamKey.sfrom, sfrom);
        String result = OkHttpUtil.postFormParams(HooUtil.getpath(path, ip), HooUtil.getParamMap(params, key));
        logger.info("{}：返回结果：{}", path, result);
        return HooUtil.handleResult(result);
    }

//    @Deprecated
//    private R<JSONObject> getApplyRam(Map<String, Object> params, String path, String ip, String key) {
//        logger.info("{}：参数：{}", path, params);
//        params.put(HooParamKey.coinName, "eos");
//        params.put(HooParamKey.num, "100");
//        params.put(HooParamKey.sfrom, sfrom);
//        String result = OkHttpUtil.postFormParams(HooUtil.getpath(path, ip), HooUtil.getParamMap(params, key));
//        logger.info("{}：返回结果：{}", path, result);
//        return HooUtil.handleResult(result);
//    }

    private R<JSONObject> applySellRam(Map<String, Object> params, String path, String ip, String key) {
        logger.info("{}：参数：{}", path, params);
        params.put(HooParamKey.coinName, "eos");
        params.put(HooParamKey.type, HooConsants.type_sell);
        String outOrderId = "hoo189" + System.currentTimeMillis();
        params.put(HooParamKey.outOrderId, outOrderId);
        params.put(HooParamKey.callBack, callBack_ip + "/applySellRam_cb");
        params.put(HooParamKey.sfrom, sfrom);
        String result = OkHttpUtil.postFormParams(HooUtil.getpath(path, ip), HooUtil.getParamMap(params, key));
        logger.info("{}：返回结果：{}", path, result);
        return HooUtil.handleResult(result);
    }

    private R<JSONObject> applyBuyRam(Map<String, Object> params, String path, String ip, String key) {
        logger.info("{}：参数：{}", path, params);
        params.put(HooParamKey.type, HooConsants.type_buy);
        String outOrderId = "hoo189" + System.currentTimeMillis();
        params.put(HooParamKey.outOrderId, outOrderId);
        params.put(HooParamKey.callBack, callBack_ip + "/applyBuyRam_cb");
        params.put(HooParamKey.sfrom, sfrom);
        String result = OkHttpUtil.postFormParams(HooUtil.getpath(path, ip), HooUtil.getParamMap(params, key));
        logger.info("{}：返回结果：{}", path, result);
        return HooUtil.handleResult(result);
    }

    private R<JSONObject> getTransactionApplyList(Map<String, Object> params, String path, String ip, String key) {
        logger.info("{}：参数：{}", path, params);
        params.put(HooParamKey.sfrom, sfrom);
        String json = OkHttpUtil.postFormParams(HooUtil.getpath(path, ip), HooUtil.getParamMap(params, key));
        logger.info("{}：返回结果：{}", path, json);
        return HooUtil.handleResult(json);
    }

    private R<JSONObject> getApplyTransaction(Map<String, Object> params, String path, String ip, String key) {
        logger.info("{}：参数：{}", path, params);
        params.put(HooParamKey.sfrom, sfrom);
        String result = OkHttpUtil.postFormParams(HooUtil.getpath(path, ip), HooUtil.getParamMap(params, key));
        logger.info("{}：返回结果：{}", path, result);
        return HooUtil.handleResult(result);
    }

    private R<JSONObject> applyTransaction(Map<String, Object> params, String path, String ip, String key) {
        logger.info("{}：参数：{}", path, params);
        String outOrderId = "hoo189" + System.currentTimeMillis();
        params.put(HooParamKey.outOrderId, outOrderId);
        params.put(HooParamKey.callBack, callBack_ip + "/applyTransaction_cb");
        params.put(HooParamKey.sfrom, sfrom);
        String result = OkHttpUtil.postFormParams(HooUtil.getpath(path, ip), HooUtil.getParamMap(params, key));
        logger.info("{}：返回结果：{}", path, result);
        return HooUtil.handleResult(result);
    }

    private R<JSONObject> getAddressApplyList(Map<String, Object> params, String path, String ip, String key) {
        logger.info("{}：参数：{}", path, params);
        params.put(HooParamKey.sfrom, sfrom);
        String result = OkHttpUtil.postFormParams(HooUtil.getpath(path, ip), HooUtil.getParamMap(params, key));
        logger.info("{}：返回结果：{}", path, result);
        return HooUtil.handleResult(result);
    }

    private R<JSONObject> getApplyAddress(Map<String, Object> params, String path, String ip, String key) {
        logger.info("{}：参数：{}", path, params);
        params.put(HooParamKey.sfrom, sfrom);
        String result = OkHttpUtil.postFormParams(HooUtil.getpath(path, ip), HooUtil.getParamMap(params, key));
        logger.info("{}：返回结果：{}", path, result);
        return HooUtil.handleResult(result);
    }

    private R<JSONObject> applyAddress(Map<String, Object> params, String path, String ip, String key) {
        logger.info("{}：参数：{}", path, params);
        if (EmptyUtil.isEmpty(params.get(HooParamKey.coinName))) {
            params.put(HooParamKey.coinName, "eth");
        }
        String outOrderId = "hoo189" + System.currentTimeMillis();
        params.put(HooParamKey.outOrderId, outOrderId);
        params.put(HooParamKey.callBack, callBack_ip + "/applyAddress_cb");
        params.put(HooParamKey.sfrom, sfrom);
        String result = OkHttpUtil.postFormParams(HooUtil.getpath(path, ip), HooUtil.getParamMap(params, key));
        logger.info("{}：返回结果：{}", path, result);
        R<JSONObject> jr = HooUtil.handleResult(result);
        jr.getData().put(HooParamKey.outOrderId, outOrderId);
        return jr;
    }
}
